package com.cpecc.dao.system;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.cpecc.model.system.YouJian;

@Repository
public class YouJianDao {

	@Resource
	private SessionFactory sessionFactory;
	
	public boolean cover(YouJian youJian){
		String hql="update YouJian t set t.title=?,t.content=?,t.state=? where t.id=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, youJian.getTitle());
		query.setString(1, youJian.getContent());
		query.setInteger(2, youJian.getState());
		query.setInteger(3, youJian.getId());
		return query.executeUpdate() > 0;
	}
	
	@SuppressWarnings("unchecked")
	public List<YouJian> list(int start,int end){
		String hql="from YouJian t where t.huanJie.id>=? and t.huanJie.id<=?";
		Query query=sessionFactory.getCurrentSession().createQuery(hql);
		query.setInteger(0, start);
		query.setInteger(1, end);
		return query.list();
	}
	
	public YouJian get(int id){
		return (YouJian) sessionFactory.getCurrentSession().get(YouJian.class, id);
	}
	
	public YouJian get2(int id){
		Session session=sessionFactory.openSession();
		YouJian youJian=new YouJian();
		String hql="from YouJian t where t.id=? and t.state=1";
		Query query=session.createQuery(hql);
		query.setInteger(0, id);
		if(query.list().size()>0){
			YouJian yj=(YouJian) query.list().get(0);
			youJian.setContent(yj.getContent());
			youJian.setTitle(yj.getTitle());
			session.close();
			return youJian;
		}
		return null;
	}
}
